package com.cqc.zxing01;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cqc.zxing01.defineview.MyImageView;
import com.cqc.zxing01.zxing.ScanListener;
import com.cqc.zxing01.zxing.ScanManager;
import com.cqc.zxing01.zxing.decode.DecodeThread;
import com.google.zxing.Result;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${cqc} on 2017/4/10.
 */

public class ScanBarCodeActivity extends AppCompatActivity {

    private Context context;
    @BindView(R.id.surfaceView)
    SurfaceView surfaceView;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.top_mask)
    RelativeLayout topMask;
    @BindView(R.id.iv_light)
    ImageView ivLight;
    @BindView(R.id.bottom_layout)
    RelativeLayout bottomLayout;
    @BindView(R.id.iv_left_mask)
    ImageView ivLeftMask;
    @BindView(R.id.iv_right_mask)
    ImageView ivRightMask;
    @BindView(R.id.capture_scan_line)
    ImageView scanLine;
    @BindView(R.id.scan_image)
    MyImageView scanImage;
    @BindView(R.id.rl_crop_view)
    RelativeLayout rlCropView;
    @BindView(R.id.rootView)
    RelativeLayout rootView;
    private ScanManager scanManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_bar_code);
        ButterKnife.bind(this);
        context = this;
        scanManager = new ScanManager(this, surfaceView, rootView, rlCropView, scanLine, DecodeThread.BARCODE_MODE, new ScanListener() {
            @Override
            public void scanResult(Result rawResult, Bundle bundle) {
                ToastUtil.showShortToast(context, rawResult.getText());
                Intent intent = new Intent();
                intent.putExtra("BarCode", rawResult.getText());
                setResult(2, intent);
                finish();
            }

            @Override
            public void scanError(Exception e) {
                ToastUtil.showShortToast(context, e.getMessage());
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        scanManager.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        scanManager.onPause();
    }

    @OnClick({R.id.iv_back, R.id.iv_light})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_light:
                scanManager.switchLight();//开启或关闭闪光灯，不用我们判断，内部已处理
                break;
        }
    }
}
